import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
    path:'/login',
    name:'Login',
    component: () => import('../views/Login.vue')
 },
 {
   path:'/home',
   name:'Home',
   redirect: '/welcome',
   component: () => import('../views/Home.vue'),
   children:[{
    path:'/userManage',
    component:() => import('../views/users/Users.vue')
  },{
    path:"/welcome",
    component:() => import('../views/Welcome.vue')

  }]
}]

const router = new VueRouter({
  routes
})

 router.beforeEach((to, from, next) => {
   const token = sessionStorage.getItem("token");
   if (token) {
     if (to.path === "/login") {
       return next("/home");
     }
     return next();
   } else {
     if (to.path === "/login") {
       return next();
     } else {
       return next("/login");
     }
   }
 });

export default router
